﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
using System.Threading;
using System.Security.Cryptography;

namespace NotificationsApp
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Timer timer, timer1;
        private int startPosX;
        private int startPosY;
        private Point initialLocation;
        private int flag;
        ExposedMethods singleton_ExposedMethod;
        public static string base_title;
        public static WebProxy proxy;
        public int windowState;
        private int firstTime;
        private Uri baseurl;

        public Form1()
        {
            InitializeComponent();
            TopMost = true;
            ShowInTaskbar = false;
            flag = 0;
            firstTime = 1;
            windowState = 0; //0 - Minimized    1-Maximized
            baseurl = null;
           // timer = new Timer();
           // timer.Interval = 50;
           // timer.Tick += timer_Tick;
        }
        private void Form1_LocationChanged(object sender, System.EventArgs e)
        {
            if (flag == 1)
            {
                Location = initialLocation;
            }
        }
        protected override void OnLoad(EventArgs e)
        {
           // startPosX = Screen.PrimaryScreen.WorkingArea.Width - Width;
           // startPosY = Screen.PrimaryScreen.WorkingArea.Height;
           // SetDesktopLocation(startPosX, startPosY);
            base.OnLoad(e);
            maximizeApp();
            //timer.Start();
        }
        delegate void minimizeAppCallback();
        delegate void maximizeAppCallback();

        public void minSafe()
        {
            minimizeAppCallback d = new minimizeAppCallback(minimizeApp);
            this.Invoke(d);
        }
        public void AppLogin()
        {
            if (windowState == 0) maxSafe();
            webBrowser1.Navigate(baseurl+"/index.php");
        }
        public void AppLogout()
        {
            if (windowState == 0) maxSafe();
            webBrowser1.Navigate(baseurl + "/logout.php");
        }
        public void maxSafe()
        {
            maximizeAppCallback d = new maximizeAppCallback(maximizeApp);
            this.Invoke(d);
        }
        public void AppNotifications()
        {
            if (windowState == 0) maxSafe();
        }
        public void minimizeApp()
        {
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1;
            timer1.Tick += timer_Tick1;
            flag = 0;
                timer1.Start();
        }
        public void maximizeApp()
        {
            startPosX = Screen.PrimaryScreen.WorkingArea.Width - Width;
            startPosY = Screen.PrimaryScreen.WorkingArea.Height;
            SetDesktopLocation(startPosX, startPosY);
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 1;
            timer.Tick += timer_Tick;
            flag = 0;
                timer.Start();
        }
        void timer_Tick1(object sender, EventArgs e)
        {
                startPosY += 10;
                if (startPosY > Screen.PrimaryScreen.WorkingArea.Height + Height)
                {
                    initialLocation = this.Location;
                    timer1.Stop();
                    flag = 1;
                    windowState = 0;
                }
                else
                {
                    SetDesktopLocation(startPosX, startPosY);
                }
        }
        void timer_Tick(object sender, EventArgs e)
        {
                startPosY -= 10;
                if (startPosY < Screen.PrimaryScreen.WorkingArea.Height - Height)
                {
                    initialLocation = this.Location;
                    //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                    flag = 1;
                    timer.Stop();
                    windowState = 1;
                    if (firstTime == 1)
                    {
                        AppLoaded();
                        firstTime = 0;
                    }
                }
                else
                {
                    SetDesktopLocation(startPosX, startPosY);
                }
        }

        private void AppLoaded()
        {
            baseurl = new Uri(System.Configuration.ConfigurationSettings.AppSettings["url"]);
            if (System.Configuration.ConfigurationSettings.AppSettings["proxy"] != null || System.Configuration.ConfigurationSettings.AppSettings["proxy"] != "")
            {
                proxy = new WebProxy(System.Configuration.ConfigurationSettings.AppSettings["proxy"]);
                System.Net.GlobalProxySelection.Select = proxy;
            }
            else
            {
                proxy = null;
            }
            singleton_ExposedMethod = new ExposedMethods(this);
            base_title = "Kiwi";
            webBrowser1.IsWebBrowserContextMenuEnabled = false;
            webBrowser1.AllowWebBrowserDrop = false;
            webBrowser1.ObjectForScripting = singleton_ExposedMethod;
            webBrowser1.Navigate(baseurl);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.InvokeScript("loaded_in_box");
        }
    }

    [ComVisible(true)]
    public class ExposedMethods
    {
        Form1 form1;
        public int loggedin;

        public ExposedMethods(Form1 temp)
        {
            form1 = temp;
            loggedin = 0;
        }
        public void nativeAlert(string mess, string cap)
        {
            MessageBox.Show(mess, cap);
        }
        public void nativeClose()
        {
            Application.Exit();
        }
        public void nativeSetPageTitle(string title)
        {
            form1.Text = Form1.base_title + " - " + title;
        }
        public string nativeGetCurrentDateTime()
        {
            return DateTime.Now.ToString();
            /*string dateTime1 = DateTime.Now.ToString();
            string[] temp = dateTime1.Split(':');
            dateTime1 = temp[0] + ":" + temp[1] + ":00 "+temp[2][3]+temp[2][4];
            return dateTime1;*/

        }
        public double nativeGetTimeDiffInMillisec(string date1, string date2)
        {
            DateTime dateTime1 = Convert.ToDateTime(date1);
            DateTime dateTime2 = Convert.ToDateTime(date2);
            double ms = dateTime1.Subtract(dateTime2).TotalMilliseconds;
            return ms;
        }
        public string nativeGetAppDataPath()
        {
            return Application.UserAppDataPath;
        }
        public void nativeCacheResource(string fpath, string url)
        {
            WebClient temp_cli = new WebClient();
            if (Form1.proxy != null) temp_cli.Proxy = Form1.proxy;
            temp_cli.DownloadFile(url, @fpath);
        }
        public int nativeFileExists(string fpath)
        {
            if (File.Exists(@fpath)) return 1;
            else return 0;
        }
        public string nativeGetTimeAddDays(string date1, double days)
        {
            DateTime dateTime1 = Convert.ToDateTime(date1);
            DateTime dateTime2 = dateTime1.AddDays(days);
            return Convert.ToString(dateTime2);
        }
        public string nativeGetTimeAddHours(string date1, double hours)
        {
            DateTime dateTime1 = Convert.ToDateTime(date1);
            DateTime dateTime2 = dateTime1.AddHours(hours);
            return Convert.ToString(dateTime2);
        }
        public string nativeGetUTCTimeFromLocalTime(string date1)
        {
            DateTime dateTime1 = Convert.ToDateTime(date1);
            DateTime dateTime2 = TimeZone.CurrentTimeZone.ToUniversalTime(dateTime1);
            return dateTime2.ToString();
        }
        public string nativeGetLocalTimeFromUTCTime(string date1)
        {
            DateTime dateTime1 = Convert.ToDateTime(date1);
            DateTime dateTime2 = TimeZone.CurrentTimeZone.ToLocalTime(dateTime1);
            return dateTime2.ToString();
        }
        public string nativeGetTimeZoneDiff()
        {
            return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).ToString();
        }
        public int nativeGetWindowstate()
        {
            return form1.windowState;
        }
        public void nativeMaximizeApp()
        {
            form1.maximizeApp();
        }
        public void nativeMinimizeApp()
        {
            form1.minimizeApp();
        }
        public void nativeLoggedIn()
        {
            loggedin = 1;
            Program.teamwebsite.loggedIn();
        }
        public void nativeLoggedOut()
        {
            loggedin = 0;
            Program.teamwebsite.loggedOut();
        }
        public string nativeGetUserName()
        {
            return Environment.UserName;
        }
        public string nativeGetDomainName()
        {
            return Environment.UserDomainName;
        }
        public string nativeGetMachineName()
        {
            return Environment.MachineName;
        }
        public void nativeCreateFileWithData(string path, string data)
        {
            System.IO.File.WriteAllText(@path, data);
        }
        public string nativeGetFileData(string path)
        {
            if (nativeFileExists(path) != 1) return "";
            return System.IO.File.ReadAllText(@path);
        }
    }
}